function request(url,data="",type="post",header="") {
	let obj = '';
	$.ajax({
		url : url,
		type : type,
		header : header,
		data : data,
		async: false,
		success : function(res) {
            obj=res;
		},error : function() {
			
		}
	});
	return obj;
}

function tips(type = "info",msg="",url="",time=2000) {
	//设置显示配置
	let messageOpts = {
		"positionClass": "toast-top-right",
		timeOut: time,
		"closeButton": false,
		"debug": false,
		"newestOnTop": true,
		"progressBar": false,
		"preventDuplicates": true,
		"onclick": null,
		"showDuration": "300",
		"hideDuration": "1000",
		"extendedTimeOut": "1000",
		"showEasing": "swing",
		"hideEasing": "linear",
		"showMethod": "fadeIn",
		"hideMethod": "fadeOut",
		"tapToDismiss": false
	};
	switch(type) {
		case 'success':
			toastr.success(msg,'',messageOpts);
			break;
		case 'error':
			toastr.error(msg,'',messageOpts);
			break;
		case 'warning ':
			toastr.warning(msg,'',messageOpts);
			break;
		default:
			toastr.info(msg,'',messageOpts);
	}

	if(url !== ""){
		setTimeout(function(){
			location.href = url;
		},time);
	}

}

// 注册
$(".registerForm").click(function(){

	let pact = $("input[name='pact']:checked").val();
	if(typeof(pact)==="undefined"){
		tips("warning","请同意服务协议");
		return false;
	}
	let account = $.trim($(".account").val());
	let pattern = /^1[3456789]\d{9}$/;
	if(!pattern.test(account)){
		tips("warning","请输入正确手机号");
		return false;
	}
	let password = $.trim($(".password").val());
	let cPassword = $.trim($(".cPassword").val());
	let pp = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
	if(!pp.test(password)){
		tips("warning","密码为6~16位字母或数字的组合");
		return false;
	}
	if(password !== cPassword){
		tips("warning","两次密码不一致");
		return false;
	}
	let code = $.trim($(".code").val());
	if(code.length === 0){
		tips("warning","请输入短信验证码");
		return false;
	}
	let res = request("/fore/doRegister",{"account": account,"password": password,"code": code})
	if(res.code === 0){
		tips("success",'注册成功',"/fore/login");
	}else{
		tips("error",res.message);
	}
});

// 登录
$(".loginForm").click(function(){
	let account = $.trim($(".account").val());
	let pattern = /^1[3456789]\d{9}$/;
	console.log(account)
	if(!pattern.test(account)){
		tips("warning","请输入正确手机号");
		return false;
	}
	let password = $.trim($(".password").val());

	let res = request("/fore/doLogin",{"account": account,"password": password})
	if(res.code === 0){
		tips("success",'登录成功',"/fore/index");
	}else{
		tips("error",res.message);
	}
});

// 修改个人信息
$(".saveUserInfoForm").click(function(){
	let nickname = $("input[name='nickname']").val();
	let sex = $("input[name='sex']:checked").val();
	let description = $("input[name='description']").val();

	let res = request("/fore/user/saveInfo",{"nickname": nickname,"sex": sex,"description": description})
	if(res.code === 0){
		tips("success",'更新成功',"/fore/user/info");
	}else{
		tips("error",res.message);
	}
});

// 修改用户密码
$(".saveUserPassword").click(function(){
	let oldPassword = $("input[name='oldPassword']").val();
	let newPassword = $("input[name='newPassword']").val();
	let conPassword = $("input[name='conPassword']").val();

	let pp = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
	if(!pp.test(newPassword)){
		tips("warning","密码为6~16位字母或数字的组合");
		return false;
	}
	if(newPassword !== conPassword){
		tips("warning","两次密码不一致");
		return false;
	}

	let res = request("/fore/user/savePassword",{"oldPassword": oldPassword,"newPassword": newPassword})
	if(res.code === 0){
		tips("success",'密码修改成成功，请重新登录',"/fore/login");
	}else{
		tips("error",res.message);
	}

});

//删除用户收货地址
$(".delAddress").click(function(){
	let id = $(this).prev().val();
	if(!id){
		tips("warning","地址不存在，请刷新页面");
		return false;
	}

	let res = request("/fore/address/delete",{"id": id})
	if(res.code === 0){
		tips("success",'删除成功',"/fore/address/list");
	}else{
		tips("error",res.message);
	}

});


// 保存用户地址信息
$(".storeAddress").click(function(){
	let contact = $("input[name='contact']").val();
	let mobile = $("input[name='mobile']").val();
	let address = $("input[name='address']").val();
	let province = $(".province option:selected").text();
	let city = $(".city option:selected").text();
	let county = $(".county option:selected").text();


	if(!contact){
		tips("warning","请输入收件人");
		return false;
	}

	let pattern = /^1[3456789]\d{9}$/;
	if(!pattern.test(mobile)){
		tips("warning","请输入正确手机号");
		return false;
	}

	if(!county){
		tips("warning","请选择收件地区");
		return false;
	}
	if(!address){
		tips("warning","请输入地址");
		return false;
	}

	let res = request("/fore/address/create",{"contact": contact,"mobile": mobile,"province": province,"city": city,"county": county,"address": address})
	if(res.code === 0){
		tips("success",'地址添加成功',"/fore/address/list");
	}else{
		tips("error",res.message);
	}

});

// 退出登录
$(".logout").click(function(){

	let res = request("/fore/user/logout",{})
	if(res.code === 0){
		tips("success",'退出登录成功',"/fore/login");
	}else{
		tips("error",res.message);
	}

});

//点击收藏
$(".collect").click(function(){

	let productId =$(".productId").val();
	if(!productId){
		tips("warning","商品不存在");
		return false;
	}
	let res = request("/fore/favorite/collect",{"productId": productId})
	if(res.code === 0){
		location.reload();
	}else{
		tips("error",res.message);
	}

});

//取消收藏
$(".cancel").click(function(){

	let productId =$(".productId").val();
	if(!productId){
		tips("warning","商品不存在");
		return false;
	}
	let res = request("/fore/favorite/cancel",{"productId": productId})
	if(res.code === 0){
		location.reload();
	}else{
		tips("error",res.message);
	}

});

//加入购物车
$(".car").click(function(){
	let productId =$(".productId").val();
	if(!productId){
		tips("warning","商品不存在");
		return false;
	}

	let specId =$(".check01 .checked").attr("spec-id");
	if(!specId){
		tips("warning","请选择商品规格");
		return false;
	}

	let number =$(".number").val();
	if(number < 1){
		tips("warning","商品数量不得小于1");
		return false;
	}

	let res = request("/fore/cart/addCart",{"productId": productId, "specId": specId, "number": number})
	if(res.code === 0){
		tips("success",'加入购物车成功');
	}else{
		tips("error",res.message);
	}

});

//商品详情页-立即购买
$(".buy").click(function(){

	let productId =$(".productId").val();
	if(!productId){
		tips("warning","商品不存在");
		return false;
	}

	let specId =$(".check01 .checked").attr("spec-id");
	if(!specId){
		tips("warning","请选择商品规格");
		return false;
	}

	let number =$(".number").val();
	if(number < 1){
		tips("warning","商品数量不得小于1");
		return false;
	}

	location.href = "/fore/order/buyByNow?productId=" + productId + "&specId=" + specId + "&number=" + number ;

});

//立即购买
$(".placeOrderByNow").click(function(){

	let productId = $(".productId").val();
	if(!productId){
		tips("warning","商品不存在");
		return false;
	}

	let specId = $(".specId").val();
	if(!specId){
		tips("warning","请选择商品规格");
		return false;
	}

	let number =$(".number").val();
	if(number < 1){
		tips("warning","商品数量不得小于1");
		return false;
	}

	let addressId =$(".address .checked").attr("address-id");
	let contact = $("input[name='contact']").val();

	if(!addressId &&  !contact){
		tips("warning","请选择收货地址");
		return false;
	}

	let data={"productId": productId, "specId": specId, "number": number,"addressId": addressId};

	if(contact){
		let mobile = $("input[name='mobile']").val();
		let address = $("input[name='address']").val();
		let province = $(".province option:selected").text();
		let city = $(".city option:selected").text();
		let county = $(".county option:selected").text();

		let pattern = /^1[3456789]\d{9}$/;
		if(!pattern.test(mobile)){
			tips("warning","请输入正确手机号");
			return false;
		}
		if(!county){
			tips("warning","请选择收件地区");
			return false;
		}
		if(!address){
			tips("warning","请输入地址");
			return false;
		}

		data={"productId": productId, "specId": specId, "number": number,"contact": contact,"mobile": mobile,"province": province,"city": city,"county": county,"address": address};
	}


	let res = request("/fore/order/placeOrderByNow",data)

	if(res.code === 0){
		let orderId = res.data.orderId;

		location.href = "/fore/pay/order?orderId=" + orderId;

	}else{
		tips("error",res.message);
	}

});


//购物车删除
$(".delCart").click(function(){

	let cartId =$(this).attr("cart-id");
	if(!cartId){
		tips("warning","商品不存在");
		return false;
	}
	let res = request("/fore/cart/delCart",{"cartId": cartId})
	if(res.code === 0){
		location.reload();
	}else{
		tips("error",res.message);
	}

});

//收藏页删除
$(".delFavorite").click(function(){

	let favoriteId =$(this).attr("favorite-id");
	if(!favoriteId){
		tips("warning","商品不存在");
		return false;
	}
	let res = request("/fore/favorite/delFavorite",{"favoriteId": favoriteId})
	if(res.code === 0){
		location.reload();
	}else{
		tips("error",res.message);
	}

});

//查询支付状态
function queryOrderPayStatus(orderId){

	if(!orderId){
		tips("warning","订单不存在");
		return false;
	}
	let res = request("/fore/pay/queryOrderPayStatus",{"orderId": orderId})
	if(res.code === 0){
		tips("success",'支付成功',"/fore/order/list");
	}else{
		tips("error",res.message);
	}

}

//购物车商品数量更改
function saveCartNumber(cartId,number){

	if(!cartId){
		tips("warning","购物车不存在");
		return false;
	}
	if(number < 1 ){
		tips("warning","数量不得小于1");
		return false;
	}
	let res = request("/fore/cart/saveCartNumber",{"cartId": cartId,"number": number})
	if(res.code !== 0){
		tips("error",res.message);
	}

}


//购物车结算商品
$(document).on("click",".btn_sty",function(){

	let val = [];
	$.each($('input[name="cart"]:checkbox:checked'),function(){
		val.push($(this).val());
	});

	let cartIds = val.join(",");
	if(!cartIds){
		tips("warning","请选择结算的商品");
		return false;
	}

	location.href = "/fore/order/buyByCart?cartIds=" + cartIds;

});

//立即购买
$(".placeOrderByCart").click(function(){

	let cartIds = $(".cartIds").val();
	if(!cartIds){
		tips("warning","商品不存在");
		return false;
	}

	let addressId =$(".address .checked").attr("address-id");
	let contact = $("input[name='contact']").val();

	if(!addressId &&  !contact){
		tips("warning","请选择收货地址");
		return false;
	}

	let data={"cartIds": cartIds,"addressId": addressId};

	if(contact){
		let mobile = $("input[name='mobile']").val();
		let address = $("input[name='address']").val();
		let province = $(".province option:selected").text();
		let city = $(".city option:selected").text();
		let county = $(".county option:selected").text();

		let pattern = /^1[3456789]\d{9}$/;
		if(!pattern.test(mobile)){
			tips("warning","请输入正确手机号");
			return false;
		}
		if(!county){
			tips("warning","请选择收件地区");
			return false;
		}
		if(!address){
			tips("warning","请输入地址");
			return false;
		}

		data={"cartIds": cartIds, "contact": contact,"mobile": mobile,"province": province,"city": city,"county": county,"address": address};
	}


	let res = request("/fore/order/placeOrderByCart",data)

	if(res.code === 0){
		let orderId = res.data.orderId;

		location.href = "/fore/pay/order?orderId=" + orderId;

	}else{
		tips("error",res.message);
	}

});

